package com.cmpan.networkdisk.controller;

import com.cmpan.networkdisk.model.Dynamic;
import com.cmpan.networkdisk.model.User;
import com.cmpan.networkdisk.service.DynamicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * ClassName:DtnamicController
 * Package:com.cmpan.networkdisk.controller
 * Description:
 *
 * @Date:2020/7/16 16:39
 * @com.chuangmei
 */
@Controller
@RequestMapping("/dynamic")
public class DynamicController {

    @Autowired
    private DynamicService dynamicService;

    @RequestMapping("/index")
    public String goDynamic(HttpServletRequest request, Model model){

        //getUser
        User user = (User) request.getSession().getAttribute("user");
        model.addAttribute("user",user);

        //查询出所有的dynamic
        List<Dynamic> dynamics = dynamicService.queryAll();

        //添加到模板页
        model.addAttribute("dynamics",dynamics);

        return "dynamic";
    }

    @GetMapping("/input")
    public String inputDynamic(){
        return "admin/inputDynamic";
    }

    @PostMapping("/adddynamic")
    public String addDynamic(Dynamic dynamic , HttpServletRequest request, Model model){

        //调用service层将dynamic写入数据库
        int count = dynamicService.addDynamic(dynamic);

        return count > 0 ? "redirect:/dynamic/index" : "error/error";
    }

}
